package com.neusoft.bigdata.hive;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 用于在MySQL中数据翻倍复制
 * 
 * @author OneCoder(OneCoder)
 * @date 2012-11-27 下午3:28:16
 */
public class HiveDataCopy {

	private static final int COPY_COUNT = 9;
	private static final long ONE_DAY_MILLISECOND = 3600 *24 *1000L;
	private static final String SQL_CREATE_TABLE_ONE = "CREATE TABLE metric_t1 AS SELECT * FROM metric;";
	private static final String SQL_CREATE_TABLE_TWO = "CREATE TABLE metric_t2 AS SELECT * FROM metric LIMIT 1;";
	private static final String SQL_DELETE_DATA_TABLE_TWO = "DELETE FROM metric_t2;";
	private static final String SQL_INSERT_DATA_TABLE_TWO_PREFIX = "from metric insert into table metric_t2 select concat(resourceid, \"_\", recordtime + ";
	private static final String SQL_INSERT_DATA_TABLE_TWO_MIDDLE = ") cpuload,memoryusage,memoryused,memorytotal,diskfree,disksize,diskioread,diskiowrite,netiowrite,netioread,memoryfree,temp1,temp2,temp3,temp4,temp5,temp6,temp7,temp8,temp9,resourceid,recordtime + ";
	private static final String SQL_INSERT_DATA_TABLE_TWO_PRSTFIX = "";
	private static final String SQL_DELETE_DATA = "DELETE FROM metric;";
	private static final String SQL_INSERT_DATA = "INSERT INTO metric SELECT * FROM (SELECT * FROM metric_t1 UNION ALL SELECT * FROM metric_t2) tmp;";
	private static final String SQL_DROP_TABLE_ONE = "DROP TABLE metric_t1;";
	private static final String SQL_DROP_TABLE_TWO = "DROP TABLE metric_t2;";
	
	

	static {
		try {
			Class.forName("org.apache.hadoop.hive.jdbc.HiveDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param args
	 * @author OneCoder(OneCoder)
	 * @throws SQLException
	 * @date 2012-11-27 下午3:28:16
	 */
	public static void main(String[] args) throws SQLException {
		Connection conn = DriverManager.getConnection("jdbc:hive://10.4.44.102:10000/default", "hive",
				"hive");
		int times = 1;
		for (int i = 1; i <= COPY_COUNT; i++) {
			System.out.println("Begin to copy: " + i +" time");
			conn.createStatement().execute(SQL_CREATE_TABLE_ONE);
			System.out.println("Create table t1 finished");
			conn.createStatement().execute(SQL_CREATE_TABLE_TWO);
			System.out.println("Create table t2 finished");
			conn.createStatement().execute(SQL_DELETE_DATA_TABLE_TWO);
			System.out.println("Delete table t2 data");
			conn.createStatement().execute(createInsertDataToTableTwoSQL(times));
			System.out.println("Insert into table t2 new data.");
			conn.createStatement().execute(SQL_DELETE_DATA);
			System.out.println("Delete table metric data");
			conn.createStatement().execute(SQL_INSERT_DATA);
			System.out.println("Insert new data into metric");
			conn.createStatement().execute(SQL_DROP_TABLE_ONE);
			System.out.println("Drop table t1");
			conn.createStatement().execute(SQL_DROP_TABLE_TWO);
			System.out.println("Drop table t2");
			times *= 2;
		}
	}
	
	private static String createCreateTempResultTableSQL(int count) {
		return "create table metric_" + count + " as select * from metric limit 1";
	}
	private static String createInsertTempResultTableSQL(int count) {
		return "create table metric_" + count + " as select * from metric limit 1";
	}

	private static String createInsertDataToTableTwoSQL(int count) {
		long addNum = count * ONE_DAY_MILLISECOND;
		StringBuilder sbuilder = new StringBuilder();
		sbuilder.append(SQL_INSERT_DATA_TABLE_TWO_PREFIX).append(addNum)
				.append(SQL_INSERT_DATA_TABLE_TWO_MIDDLE).append(addNum)
				.append(SQL_INSERT_DATA_TABLE_TWO_PRSTFIX);
		return sbuilder.toString();
	}

}
